﻿using ERP.Domain.Entities;
using ERP.Application.Goods;
using ERP.Application.Goods.GoodsDto;
using ERP.Application.Suppliercustomers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ERP.API.Controllers
{
    /// <summary>
    /// API控制器-ERP管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ERPController : ControllerBase
    {
        /// <summary>
        /// 供应商实现
        /// </summary>
        private readonly SuppliercustomerDataService suppliercustomerDataService;
        /// <summary>
        /// 商品实现
        /// </summary>
        private readonly GoodDataService goodDataService;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ERPController> logger;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="suppliercustomerDataService">供应商实现</param>
        /// <param name="goodDataService">商品实现</param>
        public ERPController(SuppliercustomerDataService suppliercustomerDataService, GoodDataService goodDataService,ILogger<ERPController> logger)
        {
            this.suppliercustomerDataService = suppliercustomerDataService;
            this.goodDataService = goodDataService;
            this.logger = logger;
        }
        /// <summary>
        /// 获取供应商信息
        /// </summary>
        /// <returns>返回供应商信息</returns>
        [HttpGet]
        public async Task<IList<SupplierCustomerInfo>> GetSupplierCustomerInfos()
        {
            return await suppliercustomerDataService.GetSupplierCustomerInfos();
        }
        /// <summary>
        /// 获取商品信息
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="keywords"></param>
        /// <param name="color"></param>
        /// <returns></returns>
        [HttpGet]
        public object GetGoods(int categoryId, string? keywords, string? color, int pageSize, int pageIndex)
        {
            return  goodDataService.GetGoods(categoryId, keywords, color,pageSize,pageIndex);
             
        }
        /// <summary>
        /// 获取商品类别信息
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        [HttpGet]
        public List<CasCader> GetCasCaders()
        {
            try
            { 
                var list= goodDataService.GetCasCaders();
                return list.ToList();
            }
            catch (Exception exp)
            {
                logger.LogError("获取商品类别信息出错" + exp.Message);
                throw;
            }
        }
        /// <summary>
        /// 添加商品分类
        /// </summary>
        /// <param name="goodType"></param>
        /// <returns>返回商品响应</returns>
        [HttpPost]
        public async Task<AddGoodTypeResponseDto> AddGoodType(AddGoodTypeRequestDto goodType)
        {
            try
            {
                return await goodDataService.AddGoodType(goodType);
            }
            catch (Exception exp)
            {
                logger.LogError("添加商品分类出错" + exp.Message);
                throw;
            }
        }
    }
}
